capture log close
clear all
set maxvar 30000
set more off
pause on
# delimit;

/* Convert outreg estimates to dta file, add means (persist) */

/* ******************** */
/* **** PREP MEANS **** */
/* ******************** */

local demo_list 	 "all male female" ; 
local demo_abbr_list "a m f" ; 

use prworapersist_crime_summstats, clear ;

keep if label == "all_mean"  	| label == "all_sd" 	| label == "all_median" | 
		label == "prec_mean" 	| label == "postt_mean"	| label == "prec_rd_mean" ;

xpose, clear varname ;

rename v1 mean_all ;
rename v2 sd_all ;
rename v3 median_all ;
rename v4 mean_trt ;
rename v5 mean_cntrl ;
rename v6 mean_rd ;

drop if _varname == "label" ;

rename _varname variables ;

order variables mean_all sd_all median_all mean_trt mean_cntrl mean_rd ;

gen demo = substr(variables, 1, 1) ;

replace variables = substr(variables,2,.);

local a_label "all" ;
local o_label "othmen" ;
local n_label "nonment" ;
local i_label "intel" ;
local p_label "phys" ;
local y_label "nonphys" ;
local t_label "twopar" ;
local s_label "singno" ;
local m_label "male" ;
local f_label "female" ;
local w_label "white" ;
local b_label "black" ;
local z_label "nonw" ;
local h_label "parhigh" ;
local l_label "parlow" ;

foreach demo in `demo_abbr_list' { ;
	
	preserve ;
	
	keep if demo == "`demo'" ;

	drop demo ;

	tempfile means_prworapersist_``demo'_label' ;

	save `means_prworapersist_``demo'_label'' ;
	
	restore ;
} ;



/* ************************************************ */
/* **** CONVERT IV ESTIMATES TO STATA DTA FILE **** */
/* ************************************************ */

foreach cov in no all{;

	foreach n of local num {;

		foreach exp in prworapersist {;
	
			if "`n'" == "1" | "`n'" == "2" {;
	
				foreach demo in `demo_list' {;
				
					import delimited `exp'_bw`bw'_crime_poly`n'`cov'cov_iv_`demo'.txt, varn(1) clear;

					egen nmcount = rownonmiss(_all), strok;

					drop if nmcount == 0 ;
					gen se = 1 if v1 == "" ;

					replace v1 = "se" + v1[_n-1] if se == 1 ;

					drop nmcount se ;

					sxpose, firstnames clear ;

					foreach var of varlist _all {;

						replace `var'   = subinstr(`var',   "*", "",.) ;
						replace `var'   = subinstr(`var',   "(", "",.) ;
						replace `var'   = subinstr(`var',   ")", "",.) ;
						replace `var'   = subinstr(`var',   ",", "",.) ;
					};
					rename VARIABLES variables ;

					quietly describe, varlist ;
					local vars `r(varlist)'  ;
					local string variables  ;
					local numeric : list vars - string ;
					display "`numeric'" ;

					foreach var of local numeric{;

						destring, replace;

					};

					gen sample = "age18unfav" if n_age18unfav != . ;
					capture replace sample = "onssi1922" if n_onssi1922 != . ;
					
					merge m:1 variables using `means_`exp'_`demo'' ;
						
					drop if _merge == 2 ;
						
					drop _merge;


					save `exp'_bw`bw'_crime_poly`n'`cov'cov_iv_`demo', replace ;

				};
			
			};

			else if "`n'" == "3" | "`n'" == "4" {;
	
				foreach demo in all {;
				
					import delimited `exp'_bw`bw'_crime_poly`n'`cov'cov_iv_`demo'.txt, varn(1) clear;

					egen nmcount = rownonmiss(_all), strok;

					drop if nmcount == 0 ;
					gen se = 1 if v1 == "" ;

					replace v1 = "se" + v1[_n-1] if se == 1 ;

					drop nmcount se ;

					sxpose, firstnames clear ;

					foreach var of varlist _all {;

						replace `var'   = subinstr(`var',   "*", "",.) ;
						replace `var'   = subinstr(`var',   "(", "",.) ;
						replace `var'   = subinstr(`var',   ")", "",.) ;
						replace `var'   = subinstr(`var',   ",", "",.) ;
					};

					rename VARIABLES variables ;

					quietly describe, varlist ;
					local vars `r(varlist)'  ;
					local string variables  ;
					local numeric : list vars - string ;
					display "`numeric'" ;

					foreach var of local numeric{;

						destring, replace;

					};

					gen sample = "age18unfav" if n_age18unfav != . ;
					capture replace sample = "onssi1922" if n_onssi1922 != . ;

					merge m:1 variables using `means_`exp'_`demo'' ;
						
					drop if _merge == 2 ;
						
					drop _merge;

					
					save `exp'_bw`bw'_crime_poly`n'`cov'cov_iv_`demo', replace ;
				};
			
			}; /* end if */
			
		}; /* end exp */
	
	}; /* end n*/

}; /* end cov*/







/* ************************************************ */
/* **** CONVERT RD ESTIMATES TO STATA DTA FILE **** */
/* ************************************************ */

foreach cov in no all{;

	foreach exp in prworapersist {;
		
		foreach n of local num {;
			
			if "`n'" == "1" | "`n'" == "2" {;
			
				foreach demo in `demo_list' {;

					import delimited `exp'_bw`bw'_crime_poly`n'`cov'cov_rd_`demo'.txt, varn(1) clear;

					egen nmcount = rownonmiss(_all), strok;

					drop if nmcount == 0 ;
					gen se = 1 if v1 == "" ;

					replace v1 = "se" + v1[_n-1] if se == 1 ;

					drop nmcount se ;

					sxpose, firstnames clear ;

					foreach var of varlist _all {;

						replace `var'   = subinstr(`var',   "*", "",.) ;
						replace `var'   = subinstr(`var',   "(", "",.) ;
						replace `var'   = subinstr(`var',   ")", "",.) ;
						replace `var'   = subinstr(`var',   ",", "",.) ;
					};

					rename VARIABLES variables ;

					quietly describe, varlist ;
					local vars `r(varlist)'  ;
					local string variables  ;
					local numeric : list vars - string ;
					display "`numeric'" ;

					foreach var of local numeric{;

						destring, replace;

					};
					
					merge m:1 variables using `means_`exp'_`demo'' ;
						
					drop if _merge == 2 ;
						
					drop _merge;

					save `exp'_bw`bw'_crime_poly`n'`cov'cov_rd_`demo', replace ;
				
				};
			
			};
			
			if "`n'" == "3" | "`n'" == "4" {;
			
				foreach demo in all {;

					import delimited `exp'_bw`bw'_crime_poly`n'`cov'cov_rd_`demo'.txt, varn(1) clear;

					egen nmcount = rownonmiss(_all), strok;

					drop if nmcount == 0 ;
					gen se = 1 if v1 == "" ;

					replace v1 = "se" + v1[_n-1] if se == 1 ;

					drop nmcount se ;

					sxpose, firstnames clear ;

					foreach var of varlist _all {;

						replace `var'   = subinstr(`var',   "*", "",.) ;
						replace `var'   = subinstr(`var',   "(", "",.) ;
						replace `var'   = subinstr(`var',   ")", "",.) ;
						replace `var'   = subinstr(`var',   ",", "",.) ;
					};

					rename VARIABLES variables ;

					#delim;
					quietly describe, varlist ;
					local vars `r(varlist)'  ;
					local string variables  ;
					local numeric : list vars - string ;
					display "`numeric'" ;

					foreach var of local numeric{;

						destring, replace;

					};
					
					merge m:1 variables using `means_`exp'_`demo'' ;
						
					drop if _merge == 2 ;
						
					drop _merge;

					save `exp'_bw`bw'_crime_poly`n'`cov'cov_rd_`demo', replace ;
				
				};
			};
		};
	};
};


